package kr.co.skplanet.commute.controller.api;

import java.util.HashMap;
import java.util.List;

import kr.co.skplanet.commute.domain.Point;
import kr.co.skplanet.commute.domain.Route;
import kr.co.skplanet.commute.domain.StopInfo;
import kr.co.skplanet.commute.exception.IllegalAuthKeyException;
import kr.co.skplanet.commute.service.BusService;
import kr.co.skplanet.commute.util.APIUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BusAPIController {
	@Autowired
	BusService busService;

	@ResponseBody
	@RequestMapping(value = { "/bus/route.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> routelist(
			@RequestParam(value = "pid", required = true) long pid
			) {
		try {
			List<Route> list = busService.getRouteList(pid);
			if(list != null)
				return APIUtils.getResultHashMap(true, "ok", list);
			else
				return APIUtils.getResultHashMap(false, "empty list");
		} catch (NullPointerException e) {
			return APIUtils.getResultHashMap(false, "invalid id");
		}
	}

	@ResponseBody
	@RequestMapping(value = { "/bus/point.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> pointlist(
			@RequestParam(value = "pid", required = true) long pid
			) {

		try {
			List<Point> list = busService.getPointList(pid);
			if(list != null)
				return APIUtils.getResultHashMap(true, "ok", list);
			else
				return APIUtils.getResultHashMap(false, "empty list");
		} catch (NullPointerException e) {
			return APIUtils.getResultHashMap(false, "invalid id");
		}
	}

	@ResponseBody
	@RequestMapping(value = { "/bus/stopinfo.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> stopinfolist(
			@RequestParam(value = "pid", required = true) long pid) {
		try {
			List<StopInfo> list = busService.getStopInfoList(pid);
			if(list != null)
				return APIUtils.getResultHashMap(true, "ok", list);
			else
				return APIUtils.getResultHashMap(false, "empty list");
		} catch (NullPointerException e) {
			return APIUtils.getResultHashMap(false, "invalid id");
		}
	}

	@ResponseBody
	@RequestMapping(value = { "/bus/saveroute.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> saveroute(
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "member_id", required = true) Long memberId,
			@RequestParam(value = "auth_key", required = true) String authKey
			) {
		Route route = new Route();
		route.setName(name);
		
		try {
			busService.saveRoute(authKey, route, memberId);
			
			if(route.getId() != 0)
				return APIUtils.getResultHashMap(true, "ok", route);
			else
				return APIUtils.getResultHashMap(false, "insert failed");
		} catch (NullPointerException e) {
			return APIUtils.getResultHashMap(false, "invalid argument");
		} catch (IllegalAuthKeyException e) {
			return APIUtils.getResultHashMap(false, "AUTHENTICATION FAILED");
		}
	}

	@ResponseBody
	@RequestMapping(value = { "/bus/savepoint.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> savepoint(
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "route_id", required = true) Long routeId,
			@RequestParam(value = "auth_key", required = true) String authKey
			) {
		Point point = new Point();
		point.setName(name);

		try {
			busService.savePoint(authKey, point, routeId);
	
			if(point.getId() != 0)
				return APIUtils.getResultHashMap(true, "ok", point);
			else
				return APIUtils.getResultHashMap(false, "insert failed");
		} catch (NullPointerException e) {
			return APIUtils.getResultHashMap(false, "invalid argument");
		} catch (IllegalAuthKeyException e) {
			return APIUtils.getResultHashMap(false, "AUTHENTICATION FAILED");
		}
	}

	@ResponseBody
	@RequestMapping(value = { "/bus/savestopinfo.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> savestopinfo(
			@RequestParam(value = "bus_id", required = true) String busId,
			@RequestParam(value = "stop_id", required = true) String stopId,
			@RequestParam(value = "bus_name", required = true) String busName,
			@RequestParam(value = "stop_name", required = true) String stopName,
			@RequestParam(value = "bus_type", required = true) String busType,
			@RequestParam(value = "point_id", required = true) Long pointId,			
			@RequestParam(value = "auth_key", required = true) String authKey
			) {
		StopInfo stopInfo = new StopInfo();
		stopInfo.setBusId(busId);
		stopInfo.setStopId(stopId);
		stopInfo.setBusName(busName);
		stopInfo.setStopName(stopName);
		stopInfo.setBusType(busType);
		
		try {
			busService.saveStopInfo(authKey, stopInfo, pointId);
	
			if(stopInfo.getId() != 0)
				return APIUtils.getResultHashMap(true, "ok", stopInfo);
			else
				return APIUtils.getResultHashMap(false, "insert failed");
		} catch (NullPointerException e) {
			return APIUtils.getResultHashMap(false, "invalid argument");
		} catch (IllegalAuthKeyException e) {
			return APIUtils.getResultHashMap(false, "AUTHENTICATION FAILED");
		}
			
	}

	@ResponseBody
	@RequestMapping(value = { "/bus/delroute.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> deleteroute(
			@RequestParam(value = "route_id", required = true) Long routeId,
			@RequestParam(value = "auth_key", required = true) String authKey
			) {
		try {
			busService.deleteRoute(authKey, routeId);
			return APIUtils.getResultHashMap(true, "delete succeed");
		} catch (IllegalArgumentException e) {
			return APIUtils.getResultHashMap(false, "IDENTITY VALUE NOT FOUND");
		} catch (IllegalAuthKeyException e) {
			return APIUtils.getResultHashMap(false, "AUTHENTICATION FAILED");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = { "/bus/delpoint.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> deletepoint(
			@RequestParam(value = "point_id", required = true) Long pointId,
			@RequestParam(value = "auth_key", required = true) String authKey
			) {
	
		try {
			busService.deletePoint(authKey, pointId);
			return APIUtils.getResultHashMap(true, "delete succeed");
		} catch (IllegalArgumentException e) {
			return APIUtils.getResultHashMap(false, "IDENTITY VALUE NOT FOUND");
		} catch (IllegalAuthKeyException e) {
			return APIUtils.getResultHashMap(false, "AUTHENTICATION FAILED");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = { "/bus/delstopinfo.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" })
	public HashMap<String, Object> deletestopinfo(
			@RequestParam(value = "stopinfo_id", required = true) Long stopInfoId,
			@RequestParam(value = "auth_key", required = true) String authKey
			) {
		
		try {
			busService.deleteStopInfo(authKey, stopInfoId);
			return APIUtils.getResultHashMap(true, "delete succeed");
		} catch (IllegalArgumentException e) {
			return APIUtils.getResultHashMap(false, "IDENTITY VALUE NOT FOUND");
		} catch (IllegalAuthKeyException e) {
			return APIUtils.getResultHashMap(false, "AUTHENTICATION FAILED");
		}
	}

}
